Skip to content

Commit 5c96a2b

Browse files
yyle88yangyile1990
authored andcommitted
Add GetLatestTag function with no-tags existence check
- Add GetLatestTag() returning (tag, exists, error) tuple - Use WithExpectExit(128, "NO-TAGS") pattern to handle missing tags - When no tags exist, return ("", false, nil) - Update demo3x example with improved log messages - Add Tag Operations section to README.md and README.zh.md - Add comprehensive tests with ZeroTags, OneTag, and TwoTags scenarios
1 parent d98fec1 commit 5c96a2b

File tree

5 files changed

+137
-6
lines changed

5 files changed

+137
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func main() {
146146

147147
gcm := gitgo.New(tempDIR)
148148
gcm.Init().Done()
149-
zaplog.SUG.Info("repo ready")
149+
zaplog.SUG.Info("repo setup complete")
150150

151151
must.Done(os.WriteFile(filepath.Join(tempDIR, "app.txt"), []byte("v1"), 0644))
152152
gcm.Add().Commit("v1").Tag("v1.0.0").Done()
@@ -160,7 +160,7 @@ func main() {
160160
zaplog.SUG.Info("latest tag:", latest)
161161

162162
count := rese.V1(gcm.GetCommitCount())
163-
zaplog.SUG.Info("total commits:", count)
163+
zaplog.SUG.Info("commit count:", count)
164164
}
165165
```
166166

@@ -198,6 +198,11 @@ func main() {
198198
- `GetRemoteURL(remote) (string, error)` - Get remote repo URL
199199
- `GetIgnoredFiles() ([]string, error)` - Get files ignored in gitignore
200200

201+
### Tag Operations
202+
203+
- `LatestGitTag() (string, error)` - Get latest tag name (fails when no tags exist)
204+
- `GetLatestTag() (string, bool, error)` - Get latest tag name with existence check
205+
201206
### Issue Handling
202207

203208
- `Result() ([]byte, error)` - Get output and check issues

README.zh.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func main() {
146146

147147
gcm := gitgo.New(tempDIR)
148148
gcm.Init().Done()
149-
zaplog.SUG.Info("repo ready")
149+
zaplog.SUG.Info("repo setup complete")
150150

151151
must.Done(os.WriteFile(filepath.Join(tempDIR, "app.txt"), []byte("v1"), 0644))
152152
gcm.Add().Commit("v1").Tag("v1.0.0").Done()
@@ -160,7 +160,7 @@ func main() {
160160
zaplog.SUG.Info("latest tag:", latest)
161161

162162
count := rese.V1(gcm.GetCommitCount())
163-
zaplog.SUG.Info("total commits:", count)
163+
zaplog.SUG.Info("commit count:", count)
164164
}
165165
```
166166

@@ -198,6 +198,11 @@ func main() {
198198
- `GetRemoteURL(remote) (string, error)` - 获取远程仓库 URL
199199
- `GetIgnoredFiles() ([]string, error)` - 获取 gitignore 忽略的文件
200200

201+
### 标签操作
202+
203+
- `LatestGitTag() (string, error)` - 获取最新标签名称(没有标签时会失败)
204+
- `GetLatestTag() (string, bool, error)` - 获取最新标签名称并检查是否存在
205+
201206
### 问题处理
202207

203208
- `Result() ([]byte, error)` - 获取输出并检查问题

git_up.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ func (G *Gcm) LatestGitTag() (string, error) {
128128
return strings.TrimSpace(string(output)), nil
129129
}
130130

131+
// GetLatestTag retrieves the latest tag name if it exists
132+
// Returns tag name, exists flag, and possible errors
133+
// When no tags exist, returns ("", false, nil)
134+
//
135+
// GetLatestTag 获取最新标签名称(如果存在)
136+
// 返回标签名称、存在标志和可能的错误
137+
// 当没有标签时,返回 ("", false, nil)
138+
func (G *Gcm) GetLatestTag() (string, bool, error) {
139+
output, exc, err := G.execConfig.NewConfig().WithExpectExit(128, "NO-TAGS").ExecTake("git", "describe", "--tags", "--abbrev=0")
140+
if err != nil {
141+
return "", false, erero.Wro(err)
142+
}
143+
switch exc {
144+
case 0:
145+
return strings.TrimSpace(string(output)), true, nil // Tag exists // 标签存在
146+
case 128:
147+
return "", false, nil // No tags exist // 没有标签
148+
default:
149+
return "", false, erero.Errorf("git describe failed with exit code %d", exc)
150+
}
151+
}
152+
131153
// LatestGitTagHasPrefix retrieves the latest tag name with the specified prefix
132154
// Returns the most recent tag that starts with the given prefix
133155
// Helps find version tags on specific subprojects and components

git_up_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,105 @@ func TestGcm_LatestGitTag_Temp(t *testing.T) {
776776
require.Equal(t, "v2.0.0", tag)
777777
}
778778

779+
// TestGetLatestTag tests GetLatestTag with 0, 1, and 2 tags scenarios
780+
// Verifies correct handling when no tags exist, one tag exists, and multiple tags exist
781+
//
782+
// TestGetLatestTag 测试 GetLatestTag 在 0、1、2 个标签场景
783+
// 验证在没有标签、1个标签和多个标签时的处理
784+
func TestGetLatestTag(t *testing.T) {
785+
t.Run("ZeroTags", func(t *testing.T) {
786+
// Create temp DIR with git repo but no tags
787+
// 创建临时目录和 git 仓库但不创建标签
788+
tempDIR := rese.V1(os.MkdirTemp("", "gitgo-get-latest-tag-zero-*"))
789+
defer func() {
790+
must.Done(os.RemoveAll(tempDIR))
791+
}()
792+
793+
gcm := gitgo.New(tempDIR)
794+
gcm.Init().Done()
795+
796+
// Create initial commit
797+
// 创建初始提交
798+
must.Done(os.WriteFile(filepath.Join(tempDIR, "init.txt"), []byte("init"), 0644))
799+
gcm.Add().Commit("initial").Done()
800+
801+
// Test GetLatestTag with no tags
802+
// 测试没有标签时的 GetLatestTag
803+
tag, exists, err := gcm.GetLatestTag()
804+
require.NoError(t, err)
805+
require.False(t, exists)
806+
require.Equal(t, "", tag)
807+
t.Log("No tags: exists=", exists)
808+
})
809+
810+
t.Run("OneTag", func(t *testing.T) {
811+
// Create temp DIR with git repo and one tag
812+
// 创建临时目录、git 仓库和一个标签
813+
tempDIR := rese.V1(os.MkdirTemp("", "gitgo-get-latest-tag-one-*"))
814+
defer func() {
815+
must.Done(os.RemoveAll(tempDIR))
816+
}()
817+
818+
gcm := gitgo.New(tempDIR)
819+
gcm.Init().Done()
820+
821+
// Create initial commit
822+
// 创建初始提交
823+
must.Done(os.WriteFile(filepath.Join(tempDIR, "init.txt"), []byte("init"), 0644))
824+
gcm.Add().Commit("initial").Done()
825+
826+
// Create one tag
827+
// 创建一个标签
828+
gcm.Tag("v0.0.1").Done()
829+
830+
// Test GetLatestTag with one tag
831+
// 测试存在一个标签时的 GetLatestTag
832+
tag, exists, err := gcm.GetLatestTag()
833+
require.NoError(t, err)
834+
require.True(t, exists)
835+
require.Equal(t, "v0.0.1", tag)
836+
t.Log("One tag: tag=", tag, "exists=", exists)
837+
})
838+
839+
t.Run("TwoTags", func(t *testing.T) {
840+
// Create temp DIR with git repo and two tags
841+
// 创建临时目录、git 仓库和两个标签
842+
tempDIR := rese.V1(os.MkdirTemp("", "gitgo-get-latest-tag-two-*"))
843+
defer func() {
844+
must.Done(os.RemoveAll(tempDIR))
845+
}()
846+
847+
gcm := gitgo.New(tempDIR)
848+
gcm.Init().Done()
849+
850+
// Create initial commit
851+
// 创建初始提交
852+
must.Done(os.WriteFile(filepath.Join(tempDIR, "init.txt"), []byte("init"), 0644))
853+
gcm.Add().Commit("initial").Done()
854+
855+
// Create first tag
856+
// 创建第一个标签
857+
gcm.Tag("v0.0.1").Done()
858+
859+
// Create second commit
860+
// 创建第二次提交
861+
must.Done(os.WriteFile(filepath.Join(tempDIR, "second.txt"), []byte("second"), 0644))
862+
gcm.Add().Commit("second").Done()
863+
864+
// Create second tag
865+
// 创建第二个标签
866+
gcm.Tag("v0.0.2").Done()
867+
868+
// Test GetLatestTag with two tags, should get the latest one
869+
// 测试存在两个标签时的 GetLatestTag,应该获取最新的标签
870+
tag, exists, err := gcm.GetLatestTag()
871+
require.NoError(t, err)
872+
require.True(t, exists)
873+
require.Equal(t, "v0.0.2", tag)
874+
t.Log("Two tags: tag=", tag, "exists=", exists)
875+
})
876+
}
877+
779878
// TestGcm_GetBranchTrackingBranch tests getting upstream branch
780879
// Verifies GetBranchTrackingBranch returns correct tracking info
781880
//

internal/demos/demo3x/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020

2121
gcm := gitgo.New(tempDIR)
2222
gcm.Init().Done()
23-
zaplog.SUG.Info("repo ready")
23+
zaplog.SUG.Info("repo setup complete")
2424

2525
must.Done(os.WriteFile(filepath.Join(tempDIR, "app.txt"), []byte("v1"), 0644))
2626
gcm.Add().Commit("v1").Tag("v1.0.0").Done()
@@ -34,5 +34,5 @@ func main() {
3434
zaplog.SUG.Info("latest tag:", latest)
3535

3636
count := rese.V1(gcm.GetCommitCount())
37-
zaplog.SUG.Info("total commits:", count)
37+
zaplog.SUG.Info("commit count:", count)
3838
}

0 commit comments

Comments
 (0)