From b7337de1a3c8a979937f4f3969209c59d052e89e Mon Sep 17 00:00:00 2001 From: chai2010 Date: Wed, 27 Apr 2016 10:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BF=A1=E6=81=AF=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0commit=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #5 --- update_version.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/update_version.go b/update_version.go index f76a792..797a95d 100644 --- a/update_version.go +++ b/update_version.go @@ -17,8 +17,10 @@ import ( ) func main() { - version := getGitCommitVersion() - data := makeVersionMarkdown(version) + version, commitTime := getGitCommitVersion() + buildTime := time.Now() + + data := makeVersionMarkdown(version, commitTime, buildTime) err := ioutil.WriteFile("./version.md", []byte(data), 0666) if err != nil { @@ -26,22 +28,24 @@ func main() { } fmt.Println("build version", version) + fmt.Println(commitTime.Format("2006-01-02")) + fmt.Println(buildTime.Format("2006-01-02")) } // 生成版本文件 -func makeVersionMarkdown(version string) string { - buildTime := time.Now().Format("2006-01-02") - +func makeVersionMarkdown(version string, commitTime, buildTime time.Time) string { return fmt.Sprintf(` ### 版本信息 - 仓库版本:[%s](gopl-zh-%s.zip) +- 更新时间:%s - 构建时间:%s `, version, version, - buildTime, + commitTime.Format("2006-01-02"), + buildTime.Format("2006-01-02"), ) } @@ -56,17 +60,23 @@ func makeVersionMarkdown(version string) string { // Merge pull request #249 from sunclx/patch-3 // // fix typo -func getGitCommitVersion() (version string) { +func getGitCommitVersion() (version string, date time.Time) { cmdOut, err := exec.Command(`git`, `log`, `-1`).CombinedOutput() if err != nil { - return "master" + return "unknown", time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC) // 第一版发布时间 } for _, line := range strings.Split(string(cmdOut), "\n") { line := strings.TrimSpace(line) if strings.HasPrefix(line, "commit") { version = strings.TrimSpace(line[len("commit"):]) - return + } + if strings.HasPrefix(line, "Date") { + const longForm = "Mon Jan 2 15:04:05 2006 -0700" + date, _ = time.Parse(longForm, strings.TrimSpace(line[len("Date:"):])) } } - return "master" + if version == "" || date.IsZero() { + return "unknown", time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC) // 第一版发布时间 + } + return }