Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,20 @@ func (a *App) runVersion(args []string, stdout, stderr io.Writer) int {
return 1
}

currentVersion := version.Current()
currentCommit := version.CurrentCommit()
out := map[string]string{
"version": version.Version,
"commit": version.Commit,
"version": currentVersion,
"commit": currentCommit,
"registryVersion": a.reg.Index.Version,
"bundleHash": a.reg.Manifest.BundleHash,
}
if *jsonOut {
_ = json.NewEncoder(stdout).Encode(out)
return 0
}
_, _ = fmt.Fprintf(stdout, "scion %s\n", version.Version)
_, _ = fmt.Fprintf(stdout, "commit: %s\n", version.Commit)
_, _ = fmt.Fprintf(stdout, "scion %s\n", currentVersion)
_, _ = fmt.Fprintf(stdout, "commit: %s\n", currentCommit)
_, _ = fmt.Fprintf(stdout, "registry: %s\n", a.reg.Index.Version)
_, _ = fmt.Fprintf(stdout, "bundle: %s\n", a.reg.Manifest.BundleHash)
return 0
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/build-bundle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func run(root, outDir string) error {
if err != nil {
return err
}
data = registry.CanonicalFileBytes(rel, data)
header := &zip.FileHeader{
Name: rel,
Method: zip.Store,
Expand Down
1 change: 1 addition & 0 deletions internal/doctor/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func checkManifestFreshness(reg *registry.Bundle, root string, add func(string,
add("error", file.Module, "bundle is stale: missing "+path)
continue
}
data = registry.CanonicalFileBytes(path, data)
sum := sha256.Sum256(data)
if hex.EncodeToString(sum[:]) != file.SHA256 {
add("error", file.Module, "bundle is stale: hash mismatch for "+path)
Expand Down
26 changes: 26 additions & 0 deletions internal/registry/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,29 @@ func matchesAny(patterns []string, rel string) bool {
}
return false
}

func CanonicalFileBytes(name string, data []byte) []byte {
if !isTextBundlePath(name) || !bytes.Contains(data, []byte{'\r'}) {
return data
}
data = bytes.ReplaceAll(data, []byte("\r\n"), []byte("\n"))
data = bytes.ReplaceAll(data, []byte("\r"), []byte("\n"))
return data
}

func isTextBundlePath(name string) bool {
base := strings.ToLower(path.Base(name))
switch base {
case ".gitignore", "dockerfile", "license", "makefile":
return true
}

switch strings.ToLower(path.Ext(name)) {
case ".css", ".env", ".go", ".html", ".js", ".json", ".jsx", ".md",
".mod", ".sql", ".sum", ".tmpl", ".tpl", ".ts", ".tsx", ".txt",
".yaml", ".yml":
return true
default:
return false
}
}
21 changes: 21 additions & 0 deletions internal/registry/canonical_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package registry

import (
"bytes"
"testing"
)

func TestCanonicalFileBytesNormalizesTextLineEndings(t *testing.T) {
data := CanonicalFileBytes("registry/cache/README.md", []byte("a\r\nb\rc\n"))
if string(data) != "a\nb\nc\n" {
t.Fatalf("unexpected canonical text: %q", data)
}
}

func TestCanonicalFileBytesLeavesBinaryExtensionsUnchanged(t *testing.T) {
in := []byte{0x89, 'P', 'N', 'G', '\r', '\n'}
out := CanonicalFileBytes("registry/assets/logo.png", in)
if !bytes.Equal(out, in) {
t.Fatalf("binary data changed: %v", out)
}
}
33 changes: 32 additions & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
package version

const (
import (
"runtime/debug"
"strings"
)

var (
// Version and Commit are overridden by release builds with -ldflags.
Version = "0.1.0-dev"
Commit = "dev"
)

func Current() string {
if Version != "" && !strings.HasSuffix(Version, "-dev") {
return Version
}
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version != "" && info.Main.Version != "(devel)" {
return info.Main.Version
}
}
return Version
}

func CurrentCommit() string {
if Commit != "" && Commit != "dev" {
return Commit
}
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" && setting.Value != "" {
return setting.Value
}
}
}
return Commit
}
Loading