-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Determine package version on a per-package basis.
This has two drawbacks currently: - The Elastic importer can't filter packages before adding them to the graph. To determine their version they would need to be imported, which would be somewhat costly. - Right now versions are int64s, representing the mtime in nanoseconds from the Go Epoch. But ElasticSearch uses the JS convention of storing large numbers in float64s, which can't handle recent MTimes without very significant loss of precision.
- Loading branch information
Showing
10 changed files
with
130 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package goref | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"golang.org/x/tools/go/loader" | ||
) | ||
|
||
// ConstantVersion returns a versionF function that always replies | ||
// with a constant version. Useful for experimenting, or for graphs | ||
// who load from an immutable snapshot of the Go universe. | ||
func ConstantVersion(v int64) (func(loader.Program, loader.PackageInfo) (int64, error)) { | ||
return func(prog loader.Program, pi loader.PackageInfo) (int64, error) { | ||
return v, nil | ||
} | ||
} | ||
|
||
// FileMTimeVersion is a versionF function that processes all files in | ||
// the provided PackageInfo and returns the newest mtime's second as a | ||
// time.Time-compatible int64. | ||
func FileMTimeVersion(prog loader.Program, pi loader.PackageInfo) (int64, error) { | ||
newestMTime := time.Time{} | ||
for _, f := range pi.Files { | ||
filepath := prog.Fset.File(f.Package).Name() | ||
fi, err := os.Stat(filepath) | ||
if err != nil { | ||
return -1, err | ||
} | ||
fmt.Printf("%s mtime: %v\n", filepath, fi.ModTime()) | ||
if fi.ModTime().After(newestMTime) { | ||
fmt.Printf("win!\n") | ||
newestMTime = fi.ModTime() | ||
} | ||
} | ||
if newestMTime == (time.Time{}) { | ||
return -1, fmt.Errorf("Unable to determine the version of package %s", pi.Pkg.Path()) | ||
} | ||
// newestMTime - epoch gives us a duration which is an int64 | ||
// of nanoseconds since the Go epoch (1/1/1 00:00:00 UTC). | ||
return int64(newestMTime.UTC().Sub(time.Time{})), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters