Skip to content

Commit

Permalink
cmd: use git-archive for version information
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Dec 7, 2022
1 parent 1f1010f commit 8b89980
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cmd/build.go export-subst
*.go diff=golang
75 changes: 49 additions & 26 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,63 @@ import (
"bytes"
"context"
"os/exec"
"runtime/debug"
"time"
)

// This is a helper for development. In production, we shouldn't assume that the
// process is running in a git repository or that git is installed. Our build
// system does this for release builds.
// Injected via git-export(1). See that man page and gitattributes(5).
const (
// Needs a length check because GitHub zipballs/tarballs don't do the
// describe and just strip the pattern.
describe = `$Format:%(describe:match=v4.*)$`
revision = `$Format:%h (%cI)$`
)

func init() {
defer func() {
if Version == "" {
Version = `???`
}
}()
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()
if Version != "" {
switch {
case Version != "":
// Had our version injected at build: do nothing.
return
}
if _, err := exec.LookPath("git"); err != nil {
// Couldn't find a git binary: do nothing.
return
}
if err := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel").Run(); err != nil {
// Couldn't find a git repository: do nothing.
return
case len(describe) > 0 && describe[0] != '$':
Version = describe
case revision[0] == '$':
// This is a helper for development. In production, we shouldn't assume
// that the process is running in a git repository or that git is
// installed. This is quite possibly wrong if run from the wrong working
// directory.
Version = `(random source build)`
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()
if _, err := exec.LookPath("git"); err != nil {
// Couldn't find a git binary: do nothing.
break
}
if err := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel").Run(); err != nil {
// Couldn't find a git repository: do nothing.
break
}
out, err := exec.CommandContext(ctx, "git", "describe").Output()
if err != nil {
// Couldn't describe the current commit: do nothing.
break
}
Version = string(bytes.TrimSpace(out))
default:
Version = revision
}
out, err := exec.CommandContext(ctx, "git", "describe").Output()
if err != nil {
// Couldn't describe the current commit: do nothing.
return

// If we can read out the current binary's debug info, append the claircore
// version if there was a replacement.
if info, ok := debug.ReadBuildInfo(); ok {
for _, m := range info.Deps {
if m.Path != "github.com/quay/claircore" {
continue
}
if m.Replace != nil && m.Replace.Version != m.Version {
Version += " (claircore " + m.Replace.Version + ")"
}
}
}
Version = string(bytes.TrimSpace(out))
}

// Version is a version string, injected at build time for release builds.
// Version is a version string, injected at release time for release builds.
var Version string

0 comments on commit 8b89980

Please sign in to comment.