cmd/gorebuild: add gorebuild version to report

For golang/go#57120.

Change-Id: Ic741fe1d856a9d853f25288ce29ad40a289653ef
Reviewed-on: https://go-review.googlesource.com/c/build/+/515356
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/gorebuild/report.go b/cmd/gorebuild/report.go
index 87efb7d..8d18ed3 100644
--- a/cmd/gorebuild/report.go
+++ b/cmd/gorebuild/report.go
@@ -11,6 +11,7 @@
 	"os"
 	"path/filepath"
 	"runtime"
+	"runtime/debug"
 	"sort"
 	"strings"
 	"sync"
@@ -20,6 +21,10 @@
 // A Report is the report about this reproduction attempt.
 // It also holds unexported state for use during the attempt.
 type Report struct {
+	Version    string // module@version of gorebuild command
+	GoVersion  string // version of go command gorebuild was built with
+	GOOS       string
+	GOARCH     string
 	Start      time.Time    // time reproduction started
 	End        time.Time    // time reproduction ended
 	Work       string       // work directory
@@ -122,12 +127,23 @@
 // Run runs the rebuilds indicated by args and returns the resulting report.
 func Run(args []string) *Report {
 	r := &Report{
-		Start: time.Now(),
-		Full:  runtime.GOOS == "linux" && runtime.GOARCH == "amd64",
+		Version:   "(unknown)",
+		GoVersion: runtime.Version(),
+		GOOS:      runtime.GOOS,
+		GOARCH:    runtime.GOARCH,
+		Start:     time.Now(),
+		Full:      runtime.GOOS == "linux" && runtime.GOARCH == "amd64",
 	}
 	defer func() {
 		r.End = time.Now()
 	}()
+	if info, ok := debug.ReadBuildInfo(); ok {
+		m := &info.Main
+		if m.Replace != nil {
+			m = m.Replace
+		}
+		r.Version = m.Path + "@" + m.Version
+	}
 
 	var err error
 	defer func() {
diff --git a/cmd/gorebuild/report.tmpl b/cmd/gorebuild/report.tmpl
index 025032c..1a485d6 100644
--- a/cmd/gorebuild/report.tmpl
+++ b/cmd/gorebuild/report.tmpl
@@ -26,6 +26,9 @@
 
 {{define "autoopen"}} {{if not (eq . "PASS")}} open {{end}} {{end}}
 
+Gorebuild version {{.Version}}.<br>
+Built with Go version {{.GoVersion}}, {{.GOOS}}-{{.GOARCH}}.<br>
+<br>
 Rebuild started at {{.Start.UTC.Format "2006-01-02 15:04:05"}} UTC.<br>
 Rebuild finished at {{.End.UTC.Format "2006-01-02 15:04:05"}} UTC.<br>
 Elapsed time: {{(.End.Sub .Start).Round 1e9}}.