From 39f3e06a80589c4c3ee6d264b87145ee267470cd Mon Sep 17 00:00:00 2001 From: "rick.chan" Date: Tue, 7 May 2024 17:53:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=20Git=20Tag=20=E4=BF=A1?= =?UTF-8?q?=E6=81=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: rick.chan --- Software/Development/Environment/Make/Makefile_简明语法.md | 6 ++++++ .../Basic/编译_Go_程序时自动添加_GitBranch_和_GitCommit.md | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Software/Development/Environment/Make/Makefile_简明语法.md b/Software/Development/Environment/Make/Makefile_简明语法.md index 8b7ff2f..e8dcfd9 100644 --- a/Software/Development/Environment/Make/Makefile_简明语法.md +++ b/Software/Development/Environment/Make/Makefile_简明语法.md @@ -70,3 +70,9 @@ GIT_BRANCH=$(shell git branch | sed -n '/\* /s///p') ```Makefile GIT_COMMIT=$(shell git log --pretty=format:'%H' -1) ``` + +### 获取最后一个 tag + +```Makefile +GIT_LAST_TAG=$(shell git describe --tags `git rev-list --tags --max-count=1`) +``` diff --git a/Software/Development/Language/Go/Basic/编译_Go_程序时自动添加_GitBranch_和_GitCommit.md b/Software/Development/Language/Go/Basic/编译_Go_程序时自动添加_GitBranch_和_GitCommit.md index ca1d2ce..f4a11d5 100644 --- a/Software/Development/Language/Go/Basic/编译_Go_程序时自动添加_GitBranch_和_GitCommit.md +++ b/Software/Development/Language/Go/Basic/编译_Go_程序时自动添加_GitBranch_和_GitCommit.md @@ -8,6 +8,7 @@ package main var ( GitBranch string GitCommit string + GitLastTag string ) func main() { @@ -15,7 +16,7 @@ func main() { } ``` -通过 go build -ldflags "-X importpath.name=value’ " 赋值字符串 value 给指定包 importpath 中的变量 name。 +通过 go build -ldflags "-X 'importpath.name=value'" 赋值字符串 value 给指定包 importpath 中的变量 name。 -ldflags 会将后边的的参数传递给 go tool link 连接器。 @@ -26,9 +27,10 @@ go tool link 的 -X 会将字符串赋值给指定包中的变量,格式是: ```makefile GIT_BRANCH=$(shell git branch | sed -n '/\* /s///p') GIT_COMMIT=$(shell git log --pretty=format:'%H' -1) +GIT_LAST_TAG=$(shell git describe --tags `git rev-list --tags --max-count=1`) target: - go build -ldflags "-s -X 'main.GitBranch=$(GIT_BRANCH)' -X 'main.GitCommit=$(GIT_COMMIT)'" + go build -ldflags "-s -X 'main.GitBranch=$(GIT_BRANCH)' -X 'main.GitCommit=$(GIT_COMMIT)' -X main.GitLastTag=$(GIT_LAST_TAG)" ``` --pretty=format 可支持: