diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1b91fd6b..475d10e3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,5 +23,6 @@ jobs: sha256sum: true project_path: cmd binary_name: scip + ldflags: "-X 'main.Reproducible=true'" asset_name: scip-${{ matrix.goos }}-${{ matrix.goarch }} extra_files: LICENSE diff --git a/cmd/main.go b/cmd/main.go index 2b4a2691..986e8fdc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,8 +1,12 @@ package main import ( + _ "embed" + "fmt" "log" "os" + "runtime/debug" + "strings" "github.com/urfave/cli/v2" ) @@ -23,10 +27,38 @@ func commands() []*cli.Command { return []*cli.Command{&convert, &lint, &print, &snapshot, &stats} } +//go:embed version.txt +var version string + +var Reproducible = "" // set by ldflags in CI + +func gitSuffix() string { + if Reproducible != "" { + return "" + } + var rev, timestamp string + clean := "true" + if buildInfo, ok := debug.ReadBuildInfo(); ok { + for _, kv := range buildInfo.Settings { + switch kv.Key { + case "vcs.revision": + rev = kv.Value + case "vcs.time": + timestamp = kv.Value + case "vcs.modified": + if kv.Value == "true" { + clean = "false" + } + } + } + } + return fmt.Sprintf("-dev\nSHA: %s\ntimestamp: %s\nclean: %s", rev, timestamp, clean) +} + func scipApp() *cli.App { app := &cli.App{ Name: "scip", - Version: "v0.2.1-git", + Version: fmt.Sprintf("v%s%s", strings.TrimSpace(version), gitSuffix()), Usage: "SCIP Code Intelligence Protocol CLI", Description: "For more details, see the project README at:\n\n\thttps://github.com/sourcegraph/scip", Commands: commands(), diff --git a/cmd/main_test.go b/cmd/main_test.go index c84488dc..62259e4d 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -19,6 +19,10 @@ import ( "github.com/sourcegraph/scip/cmd/tests/reprolang/bindings/go/repro" ) +func TestMain(m *testing.M) { + Reproducible = "true" +} + func TestCLIReferenceInSync(t *testing.T) { app := scipApp() readmeBytes, err := os.ReadFile(filepath.Join("..", "docs", "CLI.md")) diff --git a/cmd/version.txt b/cmd/version.txt new file mode 100644 index 00000000..7dff5b89 --- /dev/null +++ b/cmd/version.txt @@ -0,0 +1 @@ +0.2.1 \ No newline at end of file