From 2721f686f09eda5ad81552eeb6ec91c407e000f7 Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Sun, 6 Dec 2015 11:34:25 -0800 Subject: [PATCH] Record / Update only the major go version After some issue discussion (heroku/heroku-buildpack-go/100 and another I can't find atm) I generally think that we should really only care about the major version of Go and let downstream systems care about selecting the minor version (the user or build systems). There are probably exceptions to that, hence I also stopped auto-updating the go version on save, allowing people to specify whatever they want if they manually want to manage the file. --- Changelog.md | 4 ++++ Godeps/Godeps.json | 2 +- dep.go | 19 +++++++++++++----- diff.go | 7 +------ save.go | 37 +++++++++++++++++++++++++++++++--- update.go | 49 ++++++++++++++++++++++++++++++++++++++++------ version.go | 2 +- 7 files changed, 98 insertions(+), 22 deletions(-) diff --git a/Changelog.md b/Changelog.md index e5bb0f7..4f47dff 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,7 @@ +# v39 2015/12/16 + +* Record only the major go version (ex. go1.5) instead of the complete string. + # v38 2015/12/16 * Replace `go get`, further fix up restore error handling/reporting. diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 1ea577b..e12eee6 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,6 @@ { "ImportPath": "github.com/tools/godep", - "GoVersion": "go1.4.2", + "GoVersion": "go1.5", "Deps": [ { "ImportPath": "github.com/kr/fs", diff --git a/dep.go b/dep.go index 7ba7d30..1b4655e 100644 --- a/dep.go +++ b/dep.go @@ -68,8 +68,17 @@ func uniq(a []string) []string { return a[:i] } -// goVersion returns the version string of the Go compiler -// currently installed, e.g. "go1.1rc3". +// trimGoVersion and return the major version +func trimGoVersion(version string) (string, error) { + p := strings.Split(version, ".") + if len(p) < 2 { + return "", fmt.Errorf("Error determing major go version from: %q", version) + } + return p[0] + "." + p[1], nil +} + +// goVersion returns the major version string of the Go compiler +// currently installed, e.g. "go1.5". func goVersion() (string, error) { // Godep might have been compiled with a different // version, so we can't just use runtime.Version here. @@ -79,9 +88,9 @@ func goVersion() (string, error) { if err != nil { return "", err } - p := strings.Split(string(out), " ") - if len(p) < 3 { + gv := strings.Split(string(out), " ") + if len(gv) < 3 { return "", fmt.Errorf("Error splitting output of `go version`: Expected 3 or more elements, but there are < 3: %q", string(out)) } - return p[2], nil + return trimGoVersion(gv[2]) } diff --git a/diff.go b/diff.go index d165688..e695d40 100644 --- a/diff.go +++ b/diff.go @@ -33,14 +33,9 @@ func runDiff(cmd *Command, args []string) { log.Fatalln(err) } - ver, err := goVersion() - if err != nil { - log.Fatalln(err) - } - gnew := &Godeps{ ImportPath: dot[0].ImportPath, - GoVersion: ver, + GoVersion: gold.GoVersion, } err = gnew.fill(dot, dot[0].ImportPath) diff --git a/save.go b/save.go index d9ac2cf..b7a41b6 100644 --- a/save.go +++ b/save.go @@ -97,22 +97,25 @@ func save(pkgs []string) error { } debugln("dotPackageImportPath:", dip) - ver, err := goVersion() + cv, err := goVersion() if err != nil { return err } - debugln("goVersion:", ver) + debugln("goVersion:", cv) gold, err := loadDefaultGodepsFile() if err != nil { if !os.IsNotExist(err) { return err } + gold.GoVersion = cv } + printVersionWarnings(gold.GoVersion) + gnew := &Godeps{ ImportPath: dip, - GoVersion: ver, + GoVersion: gold.GoVersion, } switch len(pkgs) { @@ -191,6 +194,34 @@ func save(pkgs []string) error { return rewrite(a, dip, rewritePaths) } +func printVersionWarnings(ov string) { + var warning bool + cv, err := goVersion() + if err != nil { + return + } + tov, err := trimGoVersion(ov) + if err != nil { + return + } + tcv, err := trimGoVersion(cv) + if err != nil { + return + } + + if tov != ov { + log.Printf("WARNING: Recorded go version (%s) with minor version string found.\n", ov) + warning = true + } + if tcv != tov { + log.Printf("WARNING: Recorded major go version (%s) and in-use major go version (%s) differ.\n", tov, tcv) + warning = true + } + if warning { + log.Println("To record current major go version run `godep update -goversion`.") + } +} + type revError struct { ImportPath string WantRev string diff --git a/update.go b/update.go index eb35d3e..be3e5d6 100644 --- a/update.go +++ b/update.go @@ -4,6 +4,7 @@ import ( "go/parser" "go/token" "log" + "os" "path" "path/filepath" "strconv" @@ -12,30 +13,66 @@ import ( var cmdUpdate = &Command{ Name: "update", - Args: "[packages]", - Short: "use different revision of selected packages", + Args: "[-goversion] [packages]", + Short: "update selected packages or the go version", Long: ` Update changes the named dependency packages to use the revision of each currently installed in GOPATH. New code will -be copied into Godeps and the new revision will be written to -the manifest. +be copied into the Godeps workspace or vendor folder and the +new revision will be written to the manifest. -For more about specifying packages, see 'go help packages'. +If -goversion is specified, update the recorded go version. If -d is given, debug output is enabled (you probably don't want this). + +For more about specifying packages, see 'go help packages'. `, Run: runUpdate, } +var ( + updateGoVer bool +) func init() { cmdUpdate.Flag.BoolVar(&saveT, "t", false, "save test files during update") + cmdUpdate.Flag.BoolVar(&updateGoVer, "goversion", false, "update the recorded go version") } func runUpdate(cmd *Command, args []string) { - err := update(args) + if updateGoVer { + updateGoVersion() + } + if len(args) > 0 { + err := update(args) + if err != nil { + log.Fatalln(err) + } + } +} + +func updateGoVersion() { + gold, err := loadDefaultGodepsFile() + if err != nil { + if !os.IsNotExist(err) { + log.Fatalln(err) + } + } + cv, err := goVersion() + if err != nil { + log.Fatalln(err) + } + + gv := gold.GoVersion + gold.GoVersion = cv + _, err = gold.save() if err != nil { log.Fatalln(err) } + + if gv != cv { + log.Println("Updated major go version to", cv) + } + } func update(args []string) error { diff --git a/version.go b/version.go index 804635b..2b3b510 100644 --- a/version.go +++ b/version.go @@ -5,7 +5,7 @@ import ( "runtime" ) -const version = 38 +const version = 39 var cmdVersion = &Command{ Name: "version",