From 2754c91048cda1b511ae8ace1f6846d5fd9878fc Mon Sep 17 00:00:00 2001 From: Aditya Manthramurthy Date: Fri, 17 Jun 2022 11:16:51 -0700 Subject: [PATCH] Switch to last commit time based release (#4128) --- buildscripts/gen-ldflags.go | 49 +++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/buildscripts/gen-ldflags.go b/buildscripts/gen-ldflags.go index a3cf7471..5e873050 100644 --- a/buildscripts/gen-ldflags.go +++ b/buildscripts/gen-ldflags.go @@ -28,10 +28,9 @@ import ( "time" ) -func genLDFlags(now time.Time) string { - version := now.Format(time.RFC3339) - releaseTag := releaseTag(version) - copyrightYear := fmt.Sprintf("%d", now.Year()) +func genLDFlags(version string) string { + releaseTag, date := releaseTag(version) + copyrightYear := fmt.Sprintf("%d", date.Year()) var ldflagsStr string ldflagsStr = "-s -w -X github.com/minio/mc/cmd.Version=" + version + " " @@ -42,8 +41,8 @@ func genLDFlags(now time.Time) string { return ldflagsStr } -// genReleaseTag prints release tag to the console for easy git tagging. -func releaseTag(version string) string { +// releaseTag prints release tag to the console for easy git tagging. +func releaseTag(version string) (string, time.Time) { relPrefix := "DEVELOPMENT" if prefix := os.Getenv("MC_RELEASE"); prefix != "" { relPrefix = prefix @@ -52,7 +51,12 @@ func releaseTag(version string) string { relTag := strings.Replace(version, " ", "-", -1) relTag = strings.Replace(relTag, ":", "-", -1) relTag = strings.Replace(relTag, ",", "", -1) - return relPrefix + "." + relTag + t, err := time.Parse("2006-01-02T15-04-05Z", relTag) + if err != nil { + panic(err) + } + + return relPrefix + "." + relTag, t } // commitID returns the abbreviated commit-id hash of the last commit. @@ -72,6 +76,35 @@ func commitID() string { return strings.TrimSpace(string(commit)) } +func commitTime() time.Time { + // git log --format=%cD -n1 + var ( + commitUnix []byte + err error + ) + cmdName := "git" + cmdArgs := []string{"log", "--format=%cI", "-n1"} + if commitUnix, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil { + fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err) + os.Exit(1) + } + + t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(commitUnix))) + if err != nil { + fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err) + os.Exit(1) + } + + return t.UTC() +} + func main() { - fmt.Println(genLDFlags(time.Now().UTC())) + var version string + if len(os.Args) > 1 { + version = os.Args[1] + } else { + version = commitTime().Format(time.RFC3339) + } + + fmt.Println(genLDFlags(version)) }